package com.cqc.lite.service.impl;

import com.cqc.lite.dao.RoleDao;
import com.cqc.lite.domain.Role;
import com.cqc.lite.service.RoleService;
import com.cqc.lite.utils.UUIDUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    private final RoleDao roleDao;
    public RoleServiceImpl(RoleDao roleDao){
        this.roleDao=roleDao;
    }


    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public int save(Role role) {
        role.setId(UUIDUtils.getId());
        return roleDao.save(role);
    }

    @Override
    public Role findById(String id) {
        return roleDao.findById(id);
    }

    @Override
    public void addPermToRole(String roleId, List<String> permIdList) {
        //删除旧的关系
        roleDao.deleteRolePermissionByRoleId(roleId);
        //新增
        for (int i = 0; i < permIdList.size(); i++) {
            roleDao.insertRolePermission(permIdList.get(i),roleId);
        }
    }
}
